<?php
declare (strict_types=1);

namespace app\common\model;

use app\common\tool\ApiClient;

class Area extends Base
{
    public static function getArea($pid = 0, $deep = 0): object
    {
        return parent::where(['pid' => $pid, 'deep' => $deep])
            ->field('id,ext_name as title')->select();
    }

    public static function mate($deep = 0, $p = '', $c = '', $d = '', $t = ''): string
    {
        try {
            $address = '';
            switch ($deep) {
                case 4:
                    $area = [$p];
                    break;
                case 3:
                    $area = [$p, $c];
                    break;
                case 2:
                    $area = [$p, $c, $d];
                    break;
                case 1:
                    $area = [$p, $c, $d, $t];
                    break;
                default:
                    $area = [];
            }
            foreach ($area as $item) {
                if ($item) {
                    $address .= parent::where('id', $item)->value('ext_name');
                }
            }

        } catch (\Exception $e) {

            throw new \Exception($e->getMessage());
        }

        return $address;
    }

    /**
     * 根据地址获取经纬度
     */
    public static function mapLocation($province, $city, $district, $details): array
    {
        try {
            $address = '';
            if ($province) {
                $address .= parent::where(['id' => $province])->value('ext_name');
            }
            if ($city) {
                $address .= parent::where(['id' => $city])->value('ext_name');
            }
            if ($district) {
                $address .= parent::where(['id' => $district])->value('ext_name');
            }
            $address .= $details;
            $config = config('map');
            $data['get'] = [
                'key' => $config['key'],
                'address' => $address
            ];
            $result = ApiClient::create($config['domain'])->get('', $data);
            if ($result->isSuccess()) {
                $result = $result->toArray();
                if ($result['status'] != 0) {
                    throw new \Exception($result['message']);
                }
                $data = $result['result']['location'];
            } else {
                throw new \Exception('请求失败');
            }

        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => 'success', 'data' => $data];
    }

    /**
     * 根据经纬度获取地区
     */
    public static function mapCode($lat, $lng): array
    {
        try {
            $config = config('map');
            $data['get'] = [
                'key' => $config['key'],
                'location' => "$lat,$lng"
            ];
            $result = ApiClient::create($config['domain'])->get('', $data);
            if ($result->isSuccess()) {
                $result = $result->toArray();
                if ($result['status'] != 0) {
                    throw new \Exception($result['message']);
                }
                $data = $result['result']['ad_info']['adcode'];
            } else {
                throw new \Exception('请求失败');
            }

        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => 'success', 'data' => $data];
    }
}